/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import data.DataProvider;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author bertram
 */
public class Artwork {
    private String titel ="";
    private HashMap<Integer, Artist> artists = new HashMap<Integer, Artist>();
    private HashMap<Integer, ArtPhoto> photos = new HashMap<Integer,ArtPhoto>();
    private LinkedList<String> tags = new LinkedList<String>();
    private HashMap<Integer, ArtPhoto> tiffs = new HashMap<Integer, ArtPhoto>();
    private int reginID = 0;
    private int workID = 0;
    private int began = 0;
    private int finished = 0;
    private String description ="";
    private String technique ="";
    private String owner = "";
    private int type = 0;
    private boolean local = false;
    public Artwork()
    {
        try {
            this.workID = DataProvider.getInstance().getNextWork();
        } catch (SQLException ex) {
            System.out.println("kunne ikke finde næste værk id");
        }
    }
    public Artwork(int workID)
    {
        this.workID = workID;
    }

    public int getFinished() {
        return finished;
    }

    public void setFinished(int Finished) {
        this.finished = Finished;
    }

    public HashMap<Integer,Artist> getArtists() {
        return artists;
    }

    public void addArtist(Artist artist) {
        this.artists.put(artist.getArtistID(), artist);
    }

    

    public int getBegan() {
        return began;
    }

    public void setBegan(int began) {
        this.began = began;
    }

    public int getWorkID() {
        return workID;
    }

    public void setWorkID(int imageID) {
        this.workID = imageID;
    }

    

    public int getReginID() {
        return reginID;
    }

    public void setReginID(int reginID) {
        this.reginID = reginID;
    }

    public LinkedList<String> getTags() {
        return tags;
    }

    public void setTags(LinkedList<String> tags) {
        this.tags = tags;
    }

    public String getTitel() {
        return titel;
    }

    public void setTitel(String titel) {
        this.titel = titel;
    }

    public HashMap<Integer, ArtPhoto> getPhotos() {
        return photos;
        
    }
    public void addTiff(ArtPhoto pht){
        tiffs.put(pht.getImageID(), pht);
    }
    public void addPhoto(ArtPhoto pht)
    {
        if(pht.getType().equalsIgnoreCase("image/tiff")) addTiff(pht);
        else photos.put(pht.getImageID(), pht);
    }
    public ArtPhoto getWebPhoto()
    {
        for(Integer key : photos.keySet())
        {
            ArtPhoto pht = photos.get(key);
            if(pht.getType().equalsIgnoreCase("image/jpeg")) return pht;
        }
        return null;
    }

    public void addTag(String tag)
    {
        tags.add(tag);
    }

    

    public String getArtistString()
    {
        StringBuilder artistString = new StringBuilder();
        for(Artist a : artists.values())
        {
            artistString.append(a.getName()).append(", ");
        }
        return artistString.toString();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getTechnique() {
        return technique;
    }

    public void setTechnique(String technique) {
        this.technique = technique;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public boolean isLocal() {
        return local;
    }

    public void setLocal(boolean local) {
        this.local = local;
    }

    public HashMap<Integer, ArtPhoto> getTiffs() {
        return tiffs;
    }

    public void setTiffs(HashMap<Integer, ArtPhoto> tiffs) {
        this.tiffs = tiffs;
    }

    
    public JSONObject toJSON() throws JSONException
    {
        JSONObject jsonWork = new JSONObject();
        jsonWork.put("id", workID);
        jsonWork.put("title", titel);
        jsonWork.put("finished", finished);
        jsonWork.put("owner", owner);
        jsonWork.put("reginID", reginID);
        jsonWork.put("kunstnere", artists.values());
        jsonWork.put("description", description);
        jsonWork.put("photos", photos.values());
        jsonWork.put("technique", technique);
        jsonWork.put("type", type);
        jsonWork.put("tags", tags);
        return jsonWork;
    }

}
